package Text19;
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        while(n -- > 0){
            long a = scanner.nextInt();
            int q = scanner.nextInt();
            long res=qmi(a,q-2,q);
            if(a%q!=0){
                System.out.println(res);
            }else{
                System.out.println("impossible");
            }
        }
    }

    private static long qmi(long a, int k, int q) {
        long ret=1;
        while(k!=0){
            if((k&1)==1){
                ret = ret * a % q;
            }
            k=k>>1;
            a = a * a % q;
        }
        return ret;
    }
}

